//引入express
var express = require('express');
//引入路由
var apps = express.Router();
var UUID = require('uuid');
let db=require('../utils/db');

apps.get('/ceshiget1', function (req, res) {
  let sql='select * from user';
  db.query(sql,function(err,data){
    // console.log(sql);
    res.json({ val: data });
  })
})
apps.post('/ceshipost', function (req, res) {
  res.json({ val: 'yanghao' });
})


//添加banner
apps.post('/addBanner',function(req,res){
  let reqs=req.body;
  let id=UUID.v1();
  let sql='insert into banner(id,name,image_url,discribe,state,store_type,goods_id,create_time) values("'+id+'","'+reqs.values.name+'","'+reqs.imageUrl+'","'+reqs.values.discribe+'","'+reqs.values.state+'","'+reqs.values.storeType+'","'+reqs.bottomList[0].id+'","'+new Date().getTime()+'")';
  db.query(sql,function(err,data){
    if(data){
      //此处是多个商店id，需要循环添加
      for(let i in reqs.isChooseStore){
        let sql1='insert into banner_store_center(id,banner_id,store_id) values("'+UUID.v1()+'","'+id+'","'+reqs.isChooseStore[i].id+'")';
        db.query(sql1,function(err,data1){
          if(i==reqs.isChooseStore.length-1){
            res.json({
              code:200,
              data:'添加成功'
            })
          }
        })
      }
    }else{
      res.json({
        code:400,
        msg:'添加失败'
      })
    }
  })
})
apps.get('/queryStore1', function (req, res) {
  let reqs=req.query;
  let sql='select id,`name`,sell_price sellPrice,image_url imageUrl,stoke,`change`,`index` from goods limit '+(reqs.page-1)*reqs.pageSize+','+reqs.pageSize+'';
  let sql1='select count(id) count from goods'
  db.query(sql,function(err,data){
    if(data){
      db.query(sql1,function(err1,data1){
        res.json({
          code:200,
          data:{data,count:data1[0].count}
        })
      })
    }
  })
})
//查询单个商品
apps.get('/queryGoodsById', function (req, res) {
  let reqs=req.query;
  let sql='select id,name,image_url imageUrl,stoke,cost_price costPrice,supply_price supplyPrice,sell_price sellPrice from goods where id='+reqs.id;
  db.query(sql,function(err,data){
    if(data){
      res.json({
        data
      })
    }
  })
})

//查询店铺
apps.get('/queryStore', function (req, res) {
  // console.log(req.query);
  let sql='select id id,create_time createTime,name,isChoose from store where isChoose="'+req.query.isChoose+'" limit '+(req.query.page-1)*req.query.pageSize+','+req.query.pageSize+'';
  let sql1='select count(id) count from store where isChoose="'+req.query.isChoose+'"';
  db.query(sql,function(err,data){
    if(data){
      // console.log(sql);
      db.query(sql1,function(err,data1){
        res.json({
          code:200,
          data:{data,count:data1[0].count}
        })
      })
    }
  })
})

//修改是否选择商店
apps.post('/editChoose',function(req,res){
  // console.log(req.body);
  let sql='';
  if(req.body.isChoose=='选择'){
    sql='update store set isChoose="未选择" where id='+req.body.id+'';
  }else{
    sql='update store set isChoose="选择" where id='+req.body.id+'';
  }
  db.query(sql,function(err,data){
    res.json({code:'修改成功'});
  })
})

//查询banner
apps.get('/queryAllBanner',function(req,res){
  let reqs=req.query;
  console.log(reqs);
  let sql='select a.id,a.`name`,a.image_url bannerImageUrl,a.store_type storeType,a.create_time createTime,a.discribe,a.goods_id goodsId,a.state,b.`name` goodsName,b.image_url goodsImageUrl,b.sell_price sellPrice,b.information from banner a LEFT JOIN goods b ON a.goods_id=b.id where a.deleted=0 and a.`name` like "%'+reqs.inp+'%" and b.`name` like "%'+reqs.selValue+'%" limit '+(reqs.page-1)*reqs.pageSize+','+reqs.pageSize+'';
  let sql1='select count(a.id) count from banner a LEFT JOIN goods b ON a.goods_id=b.id where a.deleted=0 and a.`name` like "%'+reqs.inp+'%" and b.`name` like "%'+reqs.selValue+'%" ';
  db.query(sql,function(err,data){
    console.log(sql);
    if(data){
      db.query(sql1,function(err,data1){
        console.log(sql1);
        res.json({
          code:200,
          data:{data,count:data1[0].count}
        })
      })
    }
  })
})
//改banner启用或禁用
apps.post('/editBannerState',function(req,res){
  let sql='update banner set state='+req.body.state+' where id="'+req.body.id+'"';
  db.query(sql,function(err,data){
    res.json({code:'修改成功'});
  })
})
//删除banner
apps.post('/delBanner',function(req,res){
  let sql='update banner set deleted=1 where id="'+req.body.id+'"';
  db.query(sql,function(err,data){
    res.json({code:'删除成功'});
  })
})
apps.get('/queryBannerById',function(req,res){
  let sql='select a.name name,a.discribe discribe,a.image_url imageUrl,a.state state,a.store_type storeType,a.goods_id goodsId FROM banner a WHERE id="'+req.query.id+'"';
  db.query(sql,function(err,data){
    if(data){
      // console.log(data[0].goodsId);
      let sql1='select id,name,image_url imageUrl,stoke,cost_price costPrice,supply_price supplyPrice,sell_price sellPrice from goods where id="'+data[0].goodsId+'"';
      db.query(sql1,function(err1,data1){
        res.json({
          code:200,
          data:{
            resData1:data,
            resData2:data1
          }
        })
      })
    }
  })
})
//编辑banner
apps.post('/editBannerById',function(req,res){
  console.log(req.body);
  let reqs=req.body;
  let sql='update banner set image_url="'+reqs.imageUrl+'",goods_id="'+reqs.bottomList[0].id+'",name="'+reqs.values.name+'",discribe="'+reqs.values.discribe+'",state='+reqs.values.state+',store_type='+reqs.values.storeType+' where id="'+reqs.id+'"';
  db.query(sql,function(err,data){
    if(data){
      let sql1='delete from banner_store_center where banner_id="'+reqs.id+'"';
      db.query(sql1,function(err1,data1){
        if(data1){
          let ids=UUID.v1();
          for(let i in reqs.isChooseStore){
            let sql2='insert into banner_store_center(id,banner_id,store_id) values("'+ids+'","'+reqs.id+'","'+reqs.isChooseStore[i].id+'")';
            db.query(sql2,function(err2,data2){
              if(i==reqs.isChooseStore.length-1){
                res.json({
                  code:200,
                  data:'添加成功'
                })
              }
            })
          }
        }
      })
    }
  })
})


//c端小程序
//购物车查询
apps.get('/queryCart',function(req,res){
  let sql='SELECT a.id,c.stoke,c.`name`,c.sell_price sellPrice,c.image_url imageUrl,a.num FROM cart a LEFT JOIN `user` b on a.user_id=b.id LEFT JOIN `goods` c on a.goods_id=c.id WHERE b.id="'+req.query.id+'" and a.deleted=0';
  db.query(sql,function(err,data){
    console.log(sql);
    res.json({
      code:200,
      data:data
    })
  })
})
//通过id删除购物车
apps.post('/delCartById',function(req,res){
  let sql='update cart set deleted=1 where id="'+req.body.id+'"';
  db.query(sql,function(err,data){
    res.json({
      code:200,
      data:'删除成功！'
    })
  })
})



//b端小程序
//通过状态查询订单
apps.get('/queryOrderByStoreId',function(req,res){
  let sql='select b.`name` goodsName,a.ordernum num,a.actual_payment truePay,a.state,a.create_time createTime,(a.actual_payment-b.cost_price) income,c.`name` storeName,c.logo,d.image_url imageUrl,d.user_name userName FROM orderr a LEFT JOIN goods b on a.goods_id=b.id LEFT JOIN store c on a.store_id=c.id LEFT JOIN `user` d on d.id=a.user_id where a.deleted=0';
  db.query(sql,function(err,data){
    console.log(sql);
    res.json({
      code:200,
      data
    })
  })
})

module.exports=apps;